package com.ruicar.afs.cloud.afscase.loandealerfirstmortgage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.afscase.loandealerfirstmortgage.condition.LoanDealerFirstMortgageCondition;
import com.ruicar.afs.cloud.afscase.loandealerfirstmortgage.entity.LoanDealerFirstMortgage;
import com.ruicar.afs.cloud.afscase.loandealerfirstmortgage.vo.LoanDealerFirstMortgageVO;
import com.ruicar.afs.cloud.afscase.loandealerfirstmortgage.service.LoanDealerFirstMortgageService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.WhetherEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:rongji.zhang
 * @Date:2020/8/21 14:06
 * @Version: V1.0
 **/

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/dealerFirstMortgage")
@Api("合作商首单抵押控制")
public class LoanDealerFirstMortgageController {

    private LoanDealerFirstMortgageService loanDealerFirstMortgageService;
    /**
     * 查询所有合作商首单抵押信息
     *
     * @param condition
     * @return
     */
    @PostMapping(value = "/getDealerFirstMortgageList")
    @ApiOperation(value = "查询所有合作商首单抵押信息")
    public IResponse<IPage<LoanDealerFirstMortgageVO>> getDealerFirstMortgageList(@RequestBody LoanDealerFirstMortgageCondition condition) {
        IPage<LoanDealerFirstMortgage> dealerFirstMortgageVOList =
                loanDealerFirstMortgageService.getDealerFirstMortgageList(new Page(condition.getPageNumber(), condition.getPageSize()),condition);
        if(dealerFirstMortgageVOList != null &&dealerFirstMortgageVOList.getRecords().size() != 0){
            dealerFirstMortgageVOList.getRecords().stream().forEach(dealerFirstMortgageVO -> {
                List <LoanDealerFirstMortgage> loanDealerFirstMortgageList = loanDealerFirstMortgageService.list(Wrappers.<LoanDealerFirstMortgage>query().lambda()
                        .eq(LoanDealerFirstMortgage::getDealerNo,dealerFirstMortgageVO.getDealerNo()));
                if(loanDealerFirstMortgageList != null && loanDealerFirstMortgageList.size() != 0){
                    List <LoanDealerFirstMortgage> loanDealerFirstMortgages = loanDealerFirstMortgageService.list(Wrappers.<LoanDealerFirstMortgage>query().lambda()
                            .eq(LoanDealerFirstMortgage::getDealerNo,dealerFirstMortgageVO.getDealerNo())
                            .eq(LoanDealerFirstMortgage::getIsFirstMortgage,WhetherEnum.NO.getCode()));
                    if(loanDealerFirstMortgages != null && loanDealerFirstMortgages.size() != 0 ){
                        dealerFirstMortgageVO.setIsFirstMortgage(WhetherEnum.NO.getCode());
                    } else {
                        dealerFirstMortgageVO.setIsFirstMortgage(WhetherEnum.YES.getCode());
                    }
                } else {
                    dealerFirstMortgageVO.setIsFirstMortgage(WhetherEnum.YES.getCode());
                }
            });
        }
        return  IResponse.success(dealerFirstMortgageVOList);
    }

    /**
     * 查询单个合作商所有城市首单抵押信息
     *
     * @param condition
     * @return
     */
    @PostMapping(value = "/getDealerFirstMortgageCityList")
    @ApiOperation(value = "查询所有合作商首单抵押信息")
    public IResponse<IPage<LoanDealerFirstMortgage>> getDealerFirstMortgageCityList(@RequestBody LoanDealerFirstMortgageCondition condition) {
        IPage<LoanDealerFirstMortgage> channelAuthorizeRegionList =
                loanDealerFirstMortgageService.page(new Page(condition.getPageNumber(), condition.getPageSize()), Wrappers.<LoanDealerFirstMortgage>query().lambda()
                        .eq(LoanDealerFirstMortgage::getDealerNo, condition.getDealerNo())
                        .eq(StringUtils.isNotEmpty(condition.getProvinceCode()),LoanDealerFirstMortgage::getProvinceCode,condition.getProvinceCode())
                        .eq(StringUtils.isNotEmpty(condition.getCityCode()),LoanDealerFirstMortgage::getCityCode,condition.getCityCode()));
        return  IResponse.success(channelAuthorizeRegionList!=null ? channelAuthorizeRegionList:"");
    }

    /**
     * 更改是否首单抵押状态
     *
     * @param
     * @return
     */
    @PostMapping(value = "/modifyIsFirstMortgage")
    @ApiOperation(value = "更改是否首单抵押状态")
    @Transactional(rollbackFor = Exception.class)
    public IResponse modifyIsFirstMortgage(@RequestParam("data") String jsonDataArrStr,
                                           @RequestParam("status") String status) {
        String[] str = jsonDataArrStr.split(",");
        List idList = new ArrayList();
        for (int i = 0; i < str.length; i++) {
            idList.add(str[i]);
        }
        log.info("************************首单抵押状态修改成功*******************************");
        return IResponse.success(loanDealerFirstMortgageService.modifyIsFirstMortgage(idList, status));
    }

    /**
     * 更改是否首单抵押状态
     *
     * @param
     * @return
     */
    @PostMapping(value = "/modifyIsFirstMortgageList")
    @ApiOperation(value = "更改是否首单抵押状态")
    @Transactional(rollbackFor = Exception.class)
    public IResponse modifyIsFirstMortgageList(@RequestParam("data") String jsonDataArrStr,
                                               @RequestParam("status") String status) {
        String[] str = jsonDataArrStr.split(",");
        List channelCodeList = new ArrayList();
        for (int i = 0; i < str.length; i++) {
            channelCodeList.add(str[i]);
        }
        log.info("************************首单抵押状态修改成功*******************************");
        return IResponse.success(loanDealerFirstMortgageService.modifyIsFirstMortgageList(channelCodeList, status));
    }
}
